//################################
// 版权所有：中国农业银行软件开发中心
// 系统名称：移动办公应用-手机版
// 文件名称：splash.js
// 创建时间：2016年06月28日
// 创建人员：Panda
// 功能说明：app启动过渡页面
//################################

'use strict';

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Animated,
    InteractionManager,
    Dimensions,
    Platform
} from 'react-native';

import {
    Routes,
    CommonFunc,
    CommonData,
} from '../../common';

import LoginContainer from '../containers/loginContainer';
import GesturePasswordContainer from '../containers/gesturePasswordContainer';

import Images from '../../images';
import LocalStorage from '../../utils/storageUtil';

import {changeLoginAuth} from '../../actions';

import PandaSplashScreen from 'react-native-smart-splash-screen';

const WINDOW_WIDTH = Dimensions.get('window').width;

class SplashScreen extends Component {
    constructor(props) {
        super(props);

        this.state = {
            bounceValue: new Animated.Value(1),
        };
    }

    componentDidMount() {
        //  Splash.hide();
        if( Platform.OS == 'ios' ){
            PandaSplashScreen.close({
                animationType: PandaSplashScreen.animationType.scale,
                duration: 1000,
                delay: 1000
            });
        }else {
            PandaSplashScreen.close("scale", 1000, 1000);
        }
        setTimeout(()=> {
            CommonData.appVersion = '1.0';
            LocalStorage.getUserList((userList) => {this.gotUserList(userList)});
        }, 2000);
    }

    gotUserList(userList) {
        // 根据用户信息判断是跳转到用户名密码登录页还是跳转到手势密码登录页
        if (userList === null) {
            userList = [];
        }
        
        let isLoginWithGesturePassword = false;

        let user = {};
        for (let i = 0;i < userList.length;i++) {
            if (userList[i].lastLogined) {
                if (CommonFunc.isEmpty(userList[i].gesturePwd) === false 
                    && CommonFunc.isEmpty(userList[i].username) === false 
                    && CommonFunc.isEmpty(userList[i].password) === false) {
                    
                    isLoginWithGesturePassword = true;
                    user = userList[i];

                    break;
                }
                else {
                    // 什么都不做
                }
            }
        }

        this.navigate(isLoginWithGesturePassword, user);
    }

    navigate(isLoginWithGesturePassword, user) {
        const { navigatorRoot } = this.props;

        if (isLoginWithGesturePassword) {
            navigatorRoot.resetTo({
                name: Routes.LOGIN_WITH_GESTURE,
                component: GesturePasswordContainer,
                data: {
                    user: user,
                },
            });
        }
        else {
            navigatorRoot.resetTo({
                name: Routes.LOGIN,
                component: LoginContainer,
            });
        }
    }
    
    render() {
        return (
            <View style={styles.container}>
            </View>
        );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

export default SplashScreen;